package com.dtagri.fjwlw.ui;


import com.android.nvssdklib.PublicDefine;
import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.bean.tdvideo.cls_Channel;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Spinner;

/**
 * 天地伟业
 * @author Administrator
 *
 */
public class Show_Device_Info extends Activity{
	
	private cls_Channel m_clsChannel;
	private Bundle m_BundleChannel;
	private EditText m_editDeviceIP,m_editUserName,m_editPassWord,m_editPort,m_editChannelNO,m_editProtocolRate,m_editDeviceAddress,m_editComPort,m_editSpeed;
	private Spinner m_spinnerProtocolType,m_spinnerStreamType;

	private ImageButton m_ImagebtnSave,m_ImagebtnBack;
	private String[] strProtocol = {"DOME_PELCO_P","DOME_PELCO_D","DOME_TIANDY"};
	private String[] strComRate = {"1200","2400","4800","9600"};
	private String[] strComPort = {"Com1","Com2"};
	private String[] strStreamType = {"main","sub"};  
	
	private int m_iProtocolType,m_iStreamType;
	
	private ArrayAdapter<String> m_ProtocolAdapter,m_StreamTypeAdapter;
	  
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.video_device_info_show);
        
        initView();
	}
	
	private void initView()
	{        
		m_editDeviceIP = (EditText)this.findViewById(R.id.Device_IP);
		m_editUserName = (EditText)this.findViewById(R.id.Device_UserName);
		m_editPassWord = (EditText)this.findViewById(R.id.Device_PassWord);
		m_editPort = (EditText)this.findViewById(R.id.Device_Port);
		m_editChannelNO = (EditText)this.findViewById(R.id.Device_ChannelNO);   
		m_editProtocolRate = (EditText)this.findViewById(R.id.Device_ProtocolRate);
		m_editDeviceAddress = (EditText)this.findViewById(R.id.Device_Address);
		m_editComPort = (EditText)this.findViewById(R.id.Device_ComPort);
		m_editSpeed = (EditText)this.findViewById(R.id.Device_Speed);   
		
		m_spinnerProtocolType = (Spinner)this.findViewById(R.id.Device_ProtocolType);
		m_spinnerStreamType = (Spinner)this.findViewById(R.id.Device_StreamType);
		
		m_ImagebtnSave = (ImageButton)this.findViewById(R.id.Modif_Save);
		m_ImagebtnBack = (ImageButton)this.findViewById(R.id.Modif_Back);  
		
		m_ProtocolAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,strProtocol);
		m_StreamTypeAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,strStreamType);

		m_spinnerProtocolType.setAdapter(m_ProtocolAdapter);
		m_spinnerStreamType.setAdapter(m_StreamTypeAdapter);
		
		m_clsChannel = new cls_Channel();
		
		m_editDeviceIP.setText("59.56.92.178");
		m_editUserName.setText("admin");
		m_editPassWord.setText("1111");
		m_editPort.setText("3000");
		m_editChannelNO.setText("1");   
		m_editProtocolRate.setText("9600");
		m_editDeviceAddress.setText("1");
		m_editComPort.setText("1");
		m_editSpeed.setText("50");
		
		m_spinnerProtocolType.setSelection(0);		//2
		m_spinnerStreamType.setSelection(1);		//0
		
	       //PTZ协锟斤拷Spinner选锟斤拷锟铰硷拷锟斤拷锟斤拷
		m_spinnerProtocolType.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				switch(arg2)
				{
				case 0:
					m_iProtocolType = PublicDefine.DOME_PELCO_P;
					
					break;
				case 1:
					m_iProtocolType = PublicDefine.DOME_PELCO_D;
					
					break; 
				case 2:
					m_iProtocolType = PublicDefine.DOME_TIANDY;
					
					break;
				default:
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				m_iStreamType = PublicDefine.DOME_TIANDY;
			}});
		
    //锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
    m_spinnerStreamType.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

		public void onItemSelected(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
			// TODO Auto-generated method stub
			
			m_iStreamType = arg2;
			
		}

		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			m_iStreamType = 0;
		}});
		
        //锟斤拷锟斤拷锟斤拷锟斤拷
        m_ImagebtnSave.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				SaveInfo();	
			}
		});
        
        //锟斤拷锟斤拷
        m_ImagebtnBack.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//m_BackAlert.show();
				SaveInfo();	
			}
		});
	}
	
	//锟斤拷锟斤拷锟斤拷锟矫诧拷锟斤拷
	private boolean SaveInfo()
	{
		String strDeviceIP = m_editDeviceIP.getText().toString().trim();
		String strUserName = m_editUserName.getText().toString().trim();
		String strPassword = m_editPassWord.getText().toString().trim();
		String strDevicePort = m_editPort.getText().toString().trim();
		String strChannelNO = m_editChannelNO.getText().toString().trim();
		String strProtocolRate = m_editProtocolRate.getText().toString().trim();
		String strDeviceAddress = m_editDeviceAddress.getText().toString().trim();
		String strComPort = m_editComPort.getText().toString().trim();
		String strSpeed = m_editSpeed.getText().toString().trim();
		
		Intent intent = new Intent();
		Bundle myBundle = new Bundle();
		
		myBundle.putInt("HostID", 11);
		myBundle.putString("HostName", strDeviceIP);
		myBundle.putInt("ConnectTypeID", 0);
		myBundle.putInt("ChannelNumber", 1);
		myBundle.putString("DeviceIP", strDeviceIP);
		myBundle.putString("UserName", strUserName);
		myBundle.putString("PassWord", strPassword);
		myBundle.putInt("DevicePort", Integer.parseInt(strDevicePort));
		myBundle.putString("DeviceID", "");
		
		myBundle.putString("ChannelName", "channel"+strChannelNO);
		myBundle.putString("Protocol", strProtocolRate);
		myBundle.putInt("ChannelID", 1);
		myBundle.putInt("ChannelNo", Integer.parseInt(strChannelNO));
		myBundle.putInt("ComNumber", Integer.parseInt(strComPort));
		myBundle.putInt("ControlAddress", Integer.parseInt(strDeviceAddress));
		myBundle.putInt("ControlStep", Integer.parseInt(strSpeed));
		myBundle.putInt("RecommendID", 111);
		myBundle.putInt("RecTime",111);
		myBundle.putInt("PTZType",m_iProtocolType);
		myBundle.putInt("StreamNo", m_iStreamType);
		intent.putExtras(myBundle);
		setResult(1, intent);
		finish();
		return true;
		
	}
	//锟斤拷锟截帮拷锟斤拷锟斤拷锟?
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			//m_BackAlert.show();
			SaveInfo();	
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
